//
// shlib_LINUX.cpp
// libprotea
// Tools Library for the Protea Project / Shared Library functionality, specific to Linux
// Copyright (C) 2003, 2004, 2005 eXegeSys, Inc.
// Copyright (C) 2008 Bruce A. James
//
// This program is part of the Protea Project.
//
// The Protea Project is free software; you can redistribute it and/or modify it 
// under the terms of the GNU General Public License as published by the 
// Free Software Foundation, either version 3 of the License, or (at your 
// option) any later version.
//
// The Protea Project is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
// for more details.
// 
// You should have received a copy of the GNU General Public License along 
// with this program.  If not, see <http://www.gnu.org/licenses/>.
// 
// Please send correspondence to:
// 
// theproteaproject@gmail.com
//

#include "../OS_defines.h"
#ifdef OS_LINUX

#include "../defines.h"
#include "shlib.h"
#include "dir.h"
#include "../AppHandler/AppInterface.h"

#include "../defns.h"
using namespace ProteaApp1;

void XCSharedLibrary::Open( const char* lib_name, bool global_flag ) {
	INFUNC( XCSharedLibrary::Open, NULL );

	char* error;
	handle = dlopen( lib_name, global_flag ? RTLD_NOW | RTLD_GLOBAL : RTLD_NOW );
	if ( (error = dlerror()) ) {
		RAISETOOLSERROR_1PARAM( ERR_SL_ERROR, error );
	};

	OUTFUNC();
};



void* XCSharedLibrary::GetLibAddress( const char* name ) {
	INFUNC( XCSharedLibrary::GetLibAddress, NULL );

	void* result;

	char* error;
	result = dlsym( handle, name );
	if ( (error = dlerror()) ) {
//		cout << "SL load error:" << error << endl;
		RAISETOOLSERROR_1PARAM( ERR_SL_ERROR, error );
	};

	OUTFUNCRET( result );
};



void XCSharedLibrary::Close() {
	INFUNC( XCSharedLibrary::Close, NULL )

	if (handle) {
		char* error;

		dlclose( handle );
		if ((error = dlerror()))
			RAISETOOLSERROR_1PARAM( ERR_SL_ERROR, error );
		handle = NULL;
	};

	OUTFUNC();
};

#endif // OS_LINUX
